// pages/points/index.js
const {getPointLogs, getTotalPoint} = require('../../api/points');

Page({
  data: {
    totalPoints: 0,
    pointsList: [],
    loading: true,
    isEmpty: false,
    currentPage: 1,
    pageSize: 10,
    finished: false,
    active: 0,
    tabs: [
      { title: '全部', type: null },
      { title: '获取', type: 0 },
      { title: '使用', type: 1 }
    ],
    status: null
  },

  onLoad: function (options) {
    this.loadPointsData();
  },

  onPullDownRefresh: function () {
    this.resetAndReload();
    wx.stopPullDownRefresh();
  },

  onReachBottom: function () {
    if (!this.data.finished) {
      this.loadMorePoints();
    }
  },

  // 切换标签
  onTabChange: function (event) {
    const index = event.detail.index;
    const type = this.data.tabs[index].type;
    
    this.setData({
      active: index,
      status: type,
      currentPage: 1,
      pointsList: [],
      loading: true,
      finished: false
    });
    
    this.loadPointsData();
  },

  // 加载积分数据
  loadPointsData: function () {
    const { currentPage, pageSize, status, pointsList } = this.data;
    
    // 显示加载状态
    this.setData({ loading: true });
    getTotalPoint().then(res =>{
      if(res.code == 20000){
        this.setData({
          totalPoints: res.data,
        });
      }else {
        wx.showToast({
          title: res.msg || '总积分加载失败',
          icon: 'none'
        });
      }
    }).catch(err => {
      console.error('总积分加载失败:', err);
      wx.showToast({
        title: '总积分加载失败',
        icon: 'none'
      });
    });
    const params = {
      current: currentPage,
      pageSize: pageSize,
    }
    if (status != null) {
      params.status = status;
    }
    getPointLogs(params).then(res =>{
      if(res.code == 20000){      
        console.log(res.list);
        this.setData({
          // totalPoints: result.totalPoints,
          pointsList: [...pointsList, ...res.list],
          loading: false,
          isEmpty: currentPage === 1 && res.list.length === 0,
          finished: res.list.length < pageSize,
          currentPage: currentPage + 1
        });
      }else {
        wx.showToast({
          title: res.msg || '积分记录加载失败',
          icon: 'none'
        });
      }
     }
    ).catch(err => {
      console.error('积分记录加载失败:', err);
      wx.showToast({
        title: '积分记录加载失败',
        icon: 'none'
      });
    });
  },

  // 加载更多积分记录
  loadMorePoints: function () {
    if (!this.data.loading) {
      this.loadPointsData();
    }
  },

  // 重置并重新加载
  resetAndReload: function () {
    this.setData({
      currentPage: 1,
      pointsList: [],
      loading: true,
      finished: false
    });
    
    this.loadPointsData();
  },

  // 查看积分规则
  viewPointsRules: function () {
    wx.navigateTo({
      url: '/pages/points-rules/index'
    });
  },

  
  // 获取随机的积分获取描述
  getRandomEarnDesc: function () {
    const earnDescs = [
      '签到奖励',
      '购物奖励',
      '活动参与',
      '邀请好友',
      '评价商品',
      '完成任务',
      '新人奖励',
      '会员福利'
    ];
    
    return earnDescs[Math.floor(Math.random() * earnDescs.length)];
  },

  // 获取随机的积分使用描述
  getRandomUseDesc: function () {
    const useDescs = [
      '兑换商品',
      '抵扣订单',
      '参与抽奖',
      '兑换优惠券',
      '会员升级',
      '积分过期'
    ];
    
    return useDescs[Math.floor(Math.random() * useDescs.length)];
  },

  // 格式化日期
  formatDate: function (date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    
    return `${year}-${month}-${day}`;
  }
});